package com.cy.store.controller;

import com.cy.store.entity.Cart;
import com.cy.store.entity.vo.CartVO;
import com.cy.store.service.ICartService;
import com.cy.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController extends BaseController{
    @Autowired
    private ICartService cartService;

    @RequestMapping("/add_cart")
    public JsonResult<Void> addCart(Cart cart, HttpSession session){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        cartService.addCart(cart,username,uid);
        return new JsonResult<>(Ok);
    }

    @RequestMapping({"/",""})
    public JsonResult<List<CartVO>> showCart(HttpSession session){
        List<CartVO> data = cartService.findByUid(getUidFromSession(session));
        return new JsonResult<>(Ok,data);
    }

    @RequestMapping("{cid}/add_num")
    public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpSession session){
        Integer data = cartService.addNum(
                cid,
                getUidFromSession(session),
                getUsernameFromSession(session));
        return new JsonResult<>(Ok,data);
    }

    @RequestMapping("{cid}/reduce_num")
    public JsonResult<Integer> reduceNum(@PathVariable("cid") Integer cid,HttpSession session){
        Integer data = cartService.reduceNum(
                cid,
                getUidFromSession(session),
                getUsernameFromSession(session));
        return new JsonResult<>(Ok,data);
    }

    @RequestMapping("/show_list")
    public JsonResult<List<CartVO>> showList(Integer[] cids,HttpSession session){
        List<CartVO> data = cartService.findVoByCid(getUidFromSession(session), cids);
        return new JsonResult<>(Ok,data);
    }


}
